#include "napi/native_api.h"
#include "macro.h"

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_TRUETYPE_TABLES_H
#include FT_BITMAP_H
#include FT_STROKER_H

#include <unistd.h>

#define NAPI_FUNC_MAP_DESC(FUNC_NAME, FUNC)                                                                            \
    (napi_property_descriptor{FUNC_NAME, nullptr, FUNC, nullptr, nullptr, nullptr, napi_default, nullptr})
#define NAPI_FUNC_DESC(FUNC) NAPI_FUNC_MAP_DESC(#FUNC, FUNC)


EXTERN_C_START

static std::string napi_get_value_string_utf8(napi_env env, napi_value value) {
    size_t length = 0;
    napi_get_value_string_utf8(env, value, nullptr, 0, &length);
    if (length <= 0) {
        return "";
    }
    size_t buf_size = length + 1;
    std::string str(length, '\0');
    napi_get_value_string_utf8(env, value, (char *)str.c_str(), buf_size, &length);
    return str;
}

static napi_value napi_create_int32(napi_env env, int32_t value) {
    napi_value iret = nullptr;
    napi_create_int32(env, value, &iret);
    return iret;
}

static napi_value add(napi_env env, napi_callback_info info)
{
    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);

    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);

    napi_value sum;
    napi_create_double(env, value0 + value1, &sum);
    


    return sum;

}

static napi_value testFont(napi_env env, napi_callback_info info) {

    size_t argc = 1;
    napi_value args[1]{nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
    std::string&& font = napi_get_value_string_utf8(env, args[0]);
    

    FT_Face pFace = nullptr;

    FT_Library library = nullptr;
    FT_Init_FreeType(&library);
    
    int fontSize = 20;

    auto ret = FT_New_Face(library, font.c_str(), 0, &pFace);

    if (ret != 0) {
        LOGD("error: fail open font:%s, %d, %d, msg: %s", font.c_str(), ret, access(font.c_str(), F_OK), FT_Error_String(ret));
        return napi_create_int32(env, ret);
    }
    if (FT_Set_Pixel_Sizes(pFace, 0, fontSize) != 0) {
        LOGD("error: fail set char size:%d", fontSize);
        return napi_create_int32(env, ret);
    }
    return napi_create_int32(env, 0);
}



static napi_value Init(napi_env env, napi_value exports)
{
    napi_property_descriptor desc[] = {
        NAPI_FUNC_DESC(add),
        NAPI_FUNC_DESC(testFont)
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
    napi_module_register(&demoModule);
}
